﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExIdGen;
using RuoVea.ExUtil;
using RuoVea.Tool.AuthorizeEx;
using RuoVea.Tool.Entity;
using SqlSugar;

namespace RuoVea.Tool.Controllers
{
    /// <summary>
    /// layui皮肤生成
    /// </summary>
    public class LayuiThemeBuildController : Controller
    {
        private readonly ISqlSugarClient _repository;

        public LayuiThemeBuildController(  ISqlSugarClient sqlSugarClient)
        {
            _repository = sqlSugarClient;
        }

        ///https://layui.gitee.io/v2/docs/
        /// <summary>
        /// Layui在线主题样式生成器、在线主题样式生成工具
        /// </summary>
        /// <returns></returns>
        [Route("/layui.html")]
        public IActionResult Index()
        {
            ViewBag.Title = "Layui在线主题样式生成器、在线主题样式生成工具";
            ViewBag.keywords = "layer扩展皮肤库、layer更改皮肤、Layui在线主题样式生成器、在线主题样式生成工具、Layui主题样式、Layui样式、Layui皮肤";
            ViewBag.description = "提供layer更改皮肤，在线扩展皮肤库，在线主题皮肤的快速设置，预览，并且发布作者自己的皮肤";
            ViewBag.LayCount =  GetCount();
            return View();
        }

        [Route("/layuidoc.html")]
        public IActionResult layuidoc()
        {
            ViewBag.Title = "Layui在线文档";
            return View();
        }

        #region 使用说明
        /// <summary>
        /// 使用说明
        /// Layui在线主题样式生成器、在线样式生成器、在线主题样式生成工具使用说明
        /// </summary>
        /// <returns></returns>
        [Route("skinread.html")]
        public ActionResult LayuiSkinRead()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 皮肤演示
        /// </summary>
        /// <returns></returns>
        [Route("Themes.html")]
        public ActionResult LayuiThemes(int page = 1)
        {
            if (page < 0 || page > 2000)
                page = 1;
            Pagination parm = new Pagination { PageNo = page, Sidx = "SortOrder", PageSize = 9 };
            ViewData["page"] = page;
            ViewBag.LayCount = GetCount();
            ViewBag.pagedata = GetPage(parm);
            ViewBag.parm = parm;
            return View();
        }

        [Route("Theme.html")]
        public ActionResult LayuiTheme(string id, string sub = null,bool show=true)
        {
            ViewBag.show = show;
            if (id.NotNullOrWhiteSpace())
            {
                var resdata = _repository.Queryable<ToolLayuicssEntity>().Where(x => x.Id == id).First();
                if (resdata != null)
                    ViewBag.Title = resdata.ItemtName + "的Layui皮肤演示";
                ViewData["sub"] = sub;
                return View(resdata);
            }
           
            ViewData["sub"] = sub;
            return View();
        }

        #region Layui皮肤样式添加
        /// <summary>
        /// Layui皮肤样式添加
        /// </summary>
        /// <param name="itemtName"></param>
        /// <param name="itemContent"></param>
        /// <param name="itemText"></param>
        /// <returns></returns>
        [Authorize]
        [Route("/my/LayuiCssAdd.html")]
        public ActionResult LayuiCssAdd(string itemtName, string itemContent, string itemText)
        {
            try
            {
                if (itemContent.IsNullOrWhiteSpace())
                    return Fail("css内容不能为空.");
                if (itemText.IsNullOrWhiteSpace())
                    return Fail("css样式内容不能为空.");

                if (itemtName.IsNullOrWhiteSpace()) { itemtName = "LayuiCss" + DateTime.Now.ToString("yyyyMMddHHmmss"); }
                var userId = long.Parse(User.Claims.Where(x => x.Type == "sub").Select(x => x.Value)?.First()??"0");
                ToolLayuicssEntity entity = new ToolLayuicssEntity
                {
                    Id = IdGenerator.IdStr(""),
                    CreateBy = userId,
                    ModifyBy = userId,
                    IsDisable = 0,
                    ItemText = itemText,
                    ItemContent = itemContent,
                    CreateDate=DateTime.Now,
                    ItemtName = itemtName
                };

                var result = _repository.GetSimpleClient<ToolLayuicssEntity>().Insert(entity);
                if (result)
                    return Success("添加成功");
                else
                    return Fail("添加失败");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        #endregion



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public double GetCount()
        {
            return _repository.Queryable<ToolLayuicssEntity>().Where(x => x.IsDisable == 0).Count();
        }

        [NonAction]
        public List<ToolLayuicssEntity> GetPage(Pagination parm)
        {
            int totla = 0;
            List<ToolLayuicssEntity> query = _repository.Queryable<ToolLayuicssEntity>()
                            .OrderByIF(parm.Sidx.NotNullOrWhiteSpace(), parm.Sidx + " " + parm.Sord)
                            .ToPageList(parm.PageNo, parm.PageSize, ref totla);
            parm.Total = totla;
            return query;
        }

        /// <summary>
        /// 返回失败消息
        /// </summary>
        /// <param name="info">消息</param>
        /// <returns></returns>
        protected virtual ActionResult Fail(string info)
        {
            return Content(new RestfulResult { Code = HttpStatusCode.BadRequest, Message = info }.Json2Str());
        }

        /// <summary>
        /// 返回成功数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected virtual ActionResult Success(object data)
        {
            return Content(new RestfulResult { Code = HttpStatusCode.OK, Message = "响应成功", Data = data }.Json2Str());
        }
    }
}